//
//  SettingViewController.m
//  TextToImg
//
//  Created by Garenge on 2019/M/29.
//  Copyright © 2019 鹏鹏. All rights reserved.
//

#import "SettingViewController.h"
#import "SplitModeViewController.h"
#import "FontFamilyViewController.h"
#import "AppTool.h"
#import <SVProgressHUD/SVProgressHUD.h>

@interface SettingViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, assign) SplitMode splitMode;
@end

@implementation SettingViewController

@synthesize splitMode = _splitMode;
- (SplitMode)splitMode {
    return [[AppTool sharedAppTool] getSpliteMode];
}

- (void)setSplitMode:(SplitMode)splitMode {
    _splitMode = splitMode;
    [[AppTool sharedAppTool] saveSplitMode:splitMode];
}

- (NSArray *)dataArray {
    NSString *splitModeString = @"";
    if (self.splitMode == SplitModeWithCount) {
        splitModeString = @"按字数";
    } else if (self.splitMode == SplitModeWithString) {
        splitModeString = @"按特殊字符";
    }
    _dataArray = @[@[@{@"title" : @"分页模式",
                       @"value" : splitModeString,
                       @"func" : @"splitModeSetting"},
                       ],
                   @[@{@"title" : @"字体预览",
                       @"func" : @"viewAllFontFamilyNames"},
                   ],
                   @[@{@"title" : @"还原设置",
                       @"func" : @"resetUserSetting"},
                       ],
                   @[@{@"title" : @"检查更新",
                       @"value" : [NSString stringWithFormat:@"V%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]],
                       @"func" : @"queryForNewVersion"},
                     ],
                   @[@{@"title" : @"联系开发者",
                       @"func" : @"contactDeveloper"},
                       ],
                   ];
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubviews];
}

- (void)setUpSubviews {
    self.navigationItem.title = @"设置";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UITableView *tableiView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableiView.delegate = self;
    tableiView.dataSource = self;
    [self.view addSubview:tableiView];
    self.tableView = tableiView;
    
    tableiView.tableFooterView = [UIView new];
}

#pragma mark UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableArray *sectionMArray = self.dataArray[section];
    if (nil == sectionMArray) {
        sectionMArray = [NSMutableArray array];
    }
    return sectionMArray.count;
}

static NSString *cellIdentifier = @"cellIdentifier";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSArray *sectionArray = self.dataArray[indexPath.section];
    NSDictionary *indexInfo = sectionArray[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", indexInfo[@"title"]];
    NSString *value = indexInfo[@"value"];
    if (value.length > 0) {
        cell.detailTextLabel.text = value;
    } else {
        cell.detailTextLabel.text = @"";
    }
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *sectionArray = self.dataArray[indexPath.section];
    NSDictionary *indexInfo = sectionArray[indexPath.row];
    NSString *funcString = indexInfo[@"func"];
    if (funcString.length > 0 && [self respondsToSelector:NSSelectorFromString(funcString)]) {
        [self performSelfFuncWithString:funcString];
    } else {
        [self AlertTarget:self title:@"提示" message:@"敬请期待" sureTitle:@"知道了" sureAction:^{
            
        }];
    }
}

#pragma mark - func

#pragma mark 分页模式
- (void)splitModeSetting {
    SplitModeViewController *splitVC = [[SplitModeViewController alloc] init];
    splitVC.splitMode = self.splitMode;
    __weak typeof(self) weakSelf = self;
    splitVC.completeHander = ^(SplitMode splitMode) {
        weakSelf.splitMode = splitMode;
        [weakSelf.tableView reloadData];
    };
    [self.navigationController pushViewController:splitVC animated:YES];
}

#pragma mark 查看字体
- (void)viewAllFontFamilyNames {
    FontFamilyViewController *fontfamilyVC = [[FontFamilyViewController alloc] init];
    [self.navigationController pushViewController:fontfamilyVC animated:YES];
}

#pragma mark 还原
- (void)resetUserSetting {
    [[AppTool sharedAppTool] clearSplitMode];
    [self.tableView reloadData];
    [SVProgressHUD showSuccessWithStatus:@"已完成重置"];
}

#pragma mark 检查更新
- (void)queryForNewVersion {
//    [SVProgressHUD showSuccessWithStatus:@"敬请期待"];
//    return;
    NSString *openURL = @"https://fir.im/texttoimg";
    NSURL *URL = [NSURL URLWithString:openURL];
    /**
     ios 10 以后使用
     openURL: options: completionHandler: 这个函数异步执行，但在主队列中调用 completionHandler 中的回调
     openURL:打开的网址
     options:用来校验url和applicationConfigure是否配置正确，是否可用。
     唯一可用@{UIApplicationOpenURLOptionUniversalLinksOnly:@YES}。
     不需要就用@{}为置空，不能直接置nil。
     ompletionHandler:如不需要可置nil
     **/
    [[UIApplication sharedApplication]openURL:URL options:@{} completionHandler:^(BOOL success) {}];
}

@end
